﻿// Jinx AJAX Framework
// Copyright (C) 2008 Blake Hegerle
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using Jinx.Bindings;
using Jinx.Naming.Paths;
using Jinx.Utility;
using Jinx.Utility.Timing;

namespace Jinx.Polling
{
    public class PollingPlace : IFactory<IBinding, Path>, IDisposable
    {
        private readonly object Sync;
        private readonly TimeSpanBinder TimeSpanBinder;
        private TimeBoundOperation TimeBoundOperation;
        private List<Update> Updates;

        public PollingPlace()
        {
            Sync = new object();
            Updates = new List<Update>();
            TimeSpanBinder = new TimeSpanBinder();
            TimeSpanBinder.Start();
        }

        #region IDisposable Members

        public void Dispose()
        {
            TimeSpanBinder.SignalToStop();
            TimeSpanBinder.Join();
        }

        #endregion

        #region IFactory<IBinding,Path> Members

        public IBinding New(Path Path)
        {
            return new Binding(this, Path);
        }

        #endregion

        public void StartPoll(TimeSpan Timeout, IPoller Poller)
        {
            EndOfOperationDelegate CompletedDelegate = delegate
                                                           {
                                                               lock (Sync)
                                                               {
                                                                   Poller.OnUpdatesBecameAvailable(Updates);
                                                                   Updates = new List<Update>();
                                                                   TimeBoundOperation = null;
                                                               }
                                                           };

            EndOfOperationDelegate TimeoutDelegate = delegate
                                                         {
                                                             lock (Sync)
                                                             {
                                                                 Poller.OnTimeout();
                                                                 TimeBoundOperation = null;
                                                             }
                                                         };

            lock (Sync)
            {
                if (Updates.Count > 0)
                {
                    CompletedDelegate();
                }
                else
                {
                    TimeBoundOperation = new TimeBoundOperation(Timeout);

                    TimeBoundOperation.OnCompleted += CompletedDelegate;
                    TimeBoundOperation.OnTimeout += TimeoutDelegate;

                    TimeSpanBinder.BindOperation(TimeBoundOperation);
                }
            }
        }

        public void OnBindingUpdate(Path Path, bool Valid, object Object)
        {
            var Update = new Update
                         {
                             Path = Path,
                             Object = Object,
                             Valid = Valid
                         };

            lock (Sync)
            {
                Updates.Add(Update);

                if (TimeBoundOperation == null) return;

                TimeBoundOperation.Complete();
                TimeBoundOperation = null;
            }
        }
    }
}